﻿import pymysql
import re
import requests
import json
import time 
import get
host='sh-cdb-lksdvfsw.sql.tencentcdb.com'
port=63013
user='root'
password='zhang990113'
db='test'
tabel='hb'
def get_message():
    connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
    cursor = connection.cursor()
    #print('ok')
    sql="SELECT * FROM `%s` WHERE `r_type`= 0"%(tabel)
    cursor.execute(sql)
    result= cursor.fetchall()
    #connection.close()
    #print(result)
    if result:
        return result
    else:
        return False
def add_mysql(r_id,r_money,r_type):
    connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
    cursor = connection.cursor()

    sql1="UPDATE `%s` SET `r_money` = '%s' WHERE `%s`.`r_id` = %s;"%(tabel,str(r_money),tabel,str(r_id))
    sql2="UPDATE `%s` SET `r_type` = '%s' WHERE `%s`.`r_id` = %s;"%(tabel,str(r_type),tabel,str(r_id))
    try:    
       cursor.execute(sql1)  
       cursor.execute(sql2)  
       connection.commit()  
    except:  
       connection.rollback()  
       connection.close()
       return False 
    #connection.close()
    return True
def post_web(phone,url):
    data={
        'phone': phone,
        'url': url,
        'force':0
        }
    headers={
        'user-agent': 'Mozilla/5.0 (Linux; Android 5.1; m1 metal Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043409 Safari/537.36 V1ANDSQ7.2.5744YYBD QQ/7.2.5.3305 NetType/WIFI WebP/0.3.0 Pixel/1080',
         'x-user-token':'76B582B28EFAB6B35D61A540959F27911EF2205AA4BAFF360E421E12006445EF597FBFA6267E90454FE609636218FD54E66504A83A15F2E73BE550797A8B61E9'
         }
    web='https://mtdhb.z.xxooweb.com/user/receiving'
    html=requests.post(web,headers=headers,data=data)
    text = json.loads(html.text)
    print(text)
    if(text['code']==0):
        return text['data']['id']
    else:
        return None 
def get_web(number):
     headers={
        'user-agent': 'Mozilla/5.0 (Linux; Android 5.1; m1 metal Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043409 Safari/537.36 V1ANDSQ7.2.5744YYBD QQ/7.2.5.3305 NetType/WIFI WebP/0.3.0 Pixel/1080',
         'x-user-token':'76B582B28EFAB6B35D61A540959F27911EF2205AA4BAFF360E421E12006445EF597FBFA6267E90454FE609636218FD54E66504A83A15F2E73BE550797A8B61E9'
         }
     web=r'https://mtdhb.z.xxooweb.com/user/receiving/%s'%(str(number))
     html=requests.get(web,headers=headers)
     text = json.loads(html.text)
     print(text['data'])
     if(text):
         if(text['data']['status']==1):
             return text['data']['price']
         elif(text['data']['status']==0):
             return None
         else:
             return 0
     #return text['data'][0]['price']
def note(r_phone,r_money):
    file=open("note.txt","a")
    file.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+' '+str(r_phone)+'----'+str(r_money)+'\n')
    file.close()
 

if __name__ == "__main__" :   
    while(1):
        print("starting……")
        try:

            text=get_message()
            #print(text)
            if(text!=False):
             for message in text:
              
                r_id=message['r_id']
                
    
                #print(message['r_type'] )
                if(not message['r_type'] or message['r_type']=='0' or message['r_type']==0):
                    fp=open('record.txt','r')
                    luck=message['r_lucky']
                    
                    data=fp.readline()
                    fp.close() 
                    if(data==message['r_url']):
              
                        add_mysql(r_id,1,3)
                        continue
                    else:
                        id=get.elmhb(message['r_url'],luck)
                    print(id)
                    if(id==True):
                        add_mysql(r_id,1,1)
                        fp=open('record.txt','w')
                        fp.write(message['r_url']) 
                        fp.close()     
                        print("导入一条抢到数据")  
                    elif(id==False):
                        add_mysql(r_id,0,2)      
                        print("导入一条被抢数据")  
                    elif(id==None):
                        add_mysql(r_id,0,-2)      
                        print("导入一条错误数据")  
                                     
        except :
            add_mysql(r_id,0,-1)
            print("error")
            continue
        time.sleep(1)

    
